package com.zrrd.common.util;

/**
 * @author WanJl
 * @version 1.0
 * @title ResponseResult
 * @description 响应通用返回结果
 * @create 2024/9/5
 */
public class ResponseResult<O> {
    //响应给前端的状态码  这个状态码不是HTTP的状态
    private Integer code;

    //响应给前端的数据
    private O data;

    //响应给前端的消息
    private String message;

    public ResponseResult() {
    }

    public ResponseResult(Integer code, O data, String message) {
        this.message = message;
        this.data = data;
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public O getData() {
        return data;
    }

    public void setData(O data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }

    /**
     * 查询结果的通用响应
     * @param data
     * @return
     * @param <E>
     */
    public static <E>ResponseResult queryResponse(E data){
        if (data !=null){
            return new ResponseResult(ResponseStatusCode.FIND_OK.getCode(),data,ResponseStatusCode.FIND_OK.getMessage());
        }else{
            return new ResponseResult(ResponseStatusCode.FIND_ERR.getCode(),data,ResponseStatusCode.FIND_ERR.getMessage());
        }
    }

    /**
     * 新增功能的通用响应
     * @param data
     * @return
     * @param <E>
     */
    public static <E>ResponseResult saveResponse(E data){
        if (data !=null){
            return new ResponseResult(ResponseStatusCode.SAVE_OK.getCode(),data,ResponseStatusCode.SAVE_OK.getMessage());
        }else{
            return new ResponseResult(ResponseStatusCode.SAVE_ERR.getCode(),data,ResponseStatusCode.SAVE_ERR.getMessage());
        }
    }
    /**
     * 修改功能的通用响应
     * @param data
     * @return
     * @param <E>
     */
    public static <E>ResponseResult updateResponse(E data){
        if (data !=null){
            return new ResponseResult(ResponseStatusCode.UPDATE_OK.getCode(),data,ResponseStatusCode.UPDATE_OK.getMessage());
        }else{
            return new ResponseResult(ResponseStatusCode.UPDATE_ERR.getCode(),data,ResponseStatusCode.UPDATE_ERR.getMessage());
        }
    }
    /**
     * 删除功能的通用响应
     * @param data
     * @return
     * @param <E>
     */
    public static <E>ResponseResult deleteResponse(E data){
        if (data !=null){
            return new ResponseResult(ResponseStatusCode.DELETE_OK.getCode(),data,ResponseStatusCode.DELETE_OK.getMessage());
        }else{
            return new ResponseResult(ResponseStatusCode.DELETE_ERR.getCode(),data,ResponseStatusCode.DELETE_ERR.getMessage());
        }
    }
    public static <E>ResponseResult defaultResponse(E data){
        if (data !=null){
            return new ResponseResult(ResponseStatusCode.DEFAULT_OK.getCode(),data,ResponseStatusCode.DEFAULT_OK.getMessage());
        }else {
            return new ResponseResult(ResponseStatusCode.DEFAULT_ERR.getCode(),data,ResponseStatusCode.DEFAULT_ERR.getMessage());
        }
    }
    public static <E>ResponseResult defaultResponse(E data,String successMessage,String failMessage){
        if (data !=null){
            return new ResponseResult(ResponseStatusCode.DEFAULT_OK.getCode(),data,successMessage);
        }else {
            return new ResponseResult(ResponseStatusCode.DEFAULT_ERR.getCode(),data,failMessage);
        }
    }

}
